{extends file="./main.tpl"}
{block name=head}
<script type="text/javascript">
$(document).ready(function()
{
	$('.itemIdC').focus();
  $(".rateC").blur(function()
  {
    if($(".rateC:last").val() != '' && $(".rateC:last").val() > 0)
    {
      $("#one1").clone(true).appendTo("#mainDiv1")
      .find('input[type="text"]').val('').end()
      .find('select[option=""]').val('');
    }
  });
  $(".rateP").blur(function()
  {
    if($(".rateP:last").val() != '' && $(".rateP:last").val() > 0)
    {
      $("#one2").clone(true).appendTo("#mainDiv2")
      .find('input[type="text"]').val('').end()
      .find('select[option=""]').val('');
    }
  });
});

function setStockQuantity(thisStock)
{
	var row    = $(thisStock).parents('.itemRow1');
	var itemId = row.find('.itemIdC').val() != '' ? row.find('.itemIdC').val() : 0;
	var dataString = "itemId=" +itemId;
	$.ajax({
	  type: "GET",
	  url: "setQuntity.php",
	  data: dataString,
	  success:function(data)
	  {
	  	row.find('.quantityC').val(data);
    }
  });
}
function setUnitC(thisObj)
{
	var row    = $(thisObj).parents('.itemRow1');
	var itemId = row.find('.itemIdC').val() != '' ? row.find('.itemIdC').val() : 0;
	var dataString = "itemId=" +itemId;
	$.ajax({
	  type: "GET",
	  url: "setUnit.php",
	  data: dataString,
	  success:function(data)
	  {
	  	row.find('.getUnitC').val(data);
    }
  });
}
function setUnitP(thisObj)
{
	var row    = $(thisObj).parents('.itemRow2');
	var itemId = row.find('.itemIdP').val() != '' ? row.find('.itemIdP').val() : 0;
	var dataString = "itemId=" +itemId;
	$.ajax({
	  type: "GET",
	  url: "setUnit.php",
	  data: dataString,
	  success:function(data)
	  {
	  	row.find('.getUnitP').val(data);
    }
  });
}
function setAmountC(amountObj)
{
	var row = $(amountObj).parents('.itemRow1');
	var quantity = row.find('.quantityC').val() != '' ? row.find('.quantityC').val() : 0;
	var rate     = row.find('.rateC').val() != '' ? row.find('.rateC').val() : 0;
	var amount      = 0;
	var totalAmount = 0;
	amount = (parseFloat(quantity) * parseFloat(rate));
  row.find('.amountC').val(amount.toFixed(2));
  
  $('.amountC').each(function(i)
  {
    amount = $(this).val();
    if(!isNaN(amount))
    {
    	totalAmount += Number(amount);
    }
  });
  $('#totalAmountC').val(totalAmount.toFixed(2));
}
function setAmountP(amountObj)
{
	var row = $(amountObj).parents('.itemRow2');
	var quantity = row.find('.quantityP').val() != '' ? row.find('.quantityP').val() : 0;
	var rate     = row.find('.rateP').val() != '' ? row.find('.rateP').val() : 0;
	var amount      = 0;
	var totalAmount = 0;
	amount = (parseFloat(quantity) * parseFloat(rate));
  row.find('.amountP').val(amount.toFixed(2));
  
  $('.amountP').each(function(i)
  {
    amount = $(this).val();
    if(!isNaN(amount))
    {
    	totalAmount += Number(amount);
    }
  });
  $('#totalAmountP').val(totalAmount.toFixed(2));
}
function submitRecord()
{
	var totalAmountC = $('#totalAmountC').val();
	var totalAmountP = $('#totalAmountP').val();
	
  if(totalAmountC == totalAmountP && totalAmountC > 0 && totalAmountP > 0)
  {
	  return confirm('DO You Want To Submit !!!!!');
  }
  else
  {
  	alert('Not Match !!!!!');
  	return false;
  }
}
</script>
{/block}
{block name=body}
<form action="{$smarty.server.PHP_SELF}" method="POST" onsubmit="return submitRecord();">
<table cellpadding="2" cellspacing="0" border="1" align="center">
  <tr>
    <td valign="top">
	    <table cellpadding="2" cellspacing="0" border="1">
		    <tr>
		      <td colspan="5" align="center" style="background:#EBF5FF;">
		      	<h1><font size="5px">Consumtion</font></h1>
		      </td>
		    </tr>
		    <tr>
			    <th>Item</th>
			    <th>Unit</th>
			    <th>Quantity</th>
			    <th>Rate</th>
			    <th>Amount</th>
			  </tr>
			  <tbody id="mainDiv1">
			    <tr id="one1" class="itemRow1">
			      <td>
			        <select name="itemIdC[]" class="itemIdC" onchange="setUnitC(this); setStockQuantity(this);">
			          <option value="0">Select Item</option>
			          {html_options values=$itemArray.itemId output=$itemArray.itemName}
			        </select>
			      </td>
			      <td>
			    	  <input type="text" name="unitC[]" class="getUnitC" DISABLED />
			      </td>
			      <td>
			    	  <input type="text" name="quantityC[]" class="quantityC" onblur="setAmountC(this);" />
			      </td>
			      <td>
			    	  <input type="text" name="rateC[]" class="rateC" onblur="setAmountC(this);" />
			      </td>
			      <td>
			    	  <input type="text" name="amountC[]" class="amountC" READONLY />
			      </td>
			    </tr>
			  </tbody>
			  <tr>
			    <th colspan="4">Total</th>
			    <td align="center"><input type="text" name="totalAmountC" id="totalAmountC" class="textAlignRight" size="15" /></td>
			  </tr>
	    </table>
    </td>
    <td valign="top">
	    <table cellpadding="2" cellspacing="0" border="1">
	      <tr>
	        <td colspan="5" align="center" style="background:#EBF5FF;">
	      	  <h1><font size="5px">Production</font></h1>
	        </td>
		    </tr>
		    <tr>
			    <th>Item</th>
			    <th>Unit</th>
			    <th>Quantity</th>
			    <th>Rate</th>
			    <th>Amount</th>
			  </tr>
			  <tbody id="mainDiv2">
			    <tr id="one2" class="itemRow2">
			      <td>
			        <select name="itemIdP[]" class="itemIdP" onchange="setUnitP(this);">
			          <option value="0">Select Item</option>
			          {html_options values=$itemArray.itemId output=$itemArray.itemName}
			        </select>
			      </td>
			      <td>
			    	  <input type="text" name="unitP[]" class="getUnitP" DISABLED />
			      </td>
			      <td>
			    	  <input type="text" name="quantityP[]" class="quantityP" onblur="setAmountP(this);" />
			      </td>
			      <td>
			    	  <input type="text" name="rateP[]" class="rateP" onblur="setAmountP(this);" />
			      </td>
			      <td>
			    	  <input type="text" name="amountP[]" class="amountP" READONLY />
			      </td>
			    </tr>
			  </tbody>
			  <tr>
			    <th colspan="4">Total</th>
			    <td align="center"><input type="text" name="totalAmountP" id="totalAmountP" class="textAlignRight" size="15" /></td>
			  </tr>
			</table>
		</td>
  </tr>
  <tr>
  	<td colspan="10" align="center">
      <input type="submit" name="submitBtn" value="Save" />
    </td>
  </tr>
</table>
</form>
{/block}